package com.bytedance.frameworks.baselib.network.http.a;

import android.text.TextUtils;

import com.bytedance.common.utility.Logger;

import java.io.IOException;
import java.net.CookieManager;
import java.net.URI;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public final class SSCookieHandler extends CookieManager {
    private final android.webkit.CookieManager a;

    public SSCookieHandler(android.webkit.CookieManager arg1) {
        this.a = arg1;
    }

    private static List a(Map arg2, String arg3) {
        Object v0 = null;
        if (arg2 == null || (TextUtils.isEmpty(((CharSequence) arg3)))) {
            v0 = null;
        } else {
            v0 = arg2.get(arg3);
            if (v0 != null && !((List) v0).isEmpty()) {
//                goto label_4;
            } else {
                v0 = arg2.get(arg3.toLowerCase());
            }
        }

//    label_4:
        return ((List) v0);
    }

    public final Map get(URI arg6, Map arg7) throws IOException {
        Map v0_3;
        if (arg6 != null && this.a != null) {
            try {
                String v1 = arg6.toString();
                if (arg7 != null) {
                    List v0_1 = a(arg7, "X-SS-No-Cookie");
                    if (v0_1 != null) {
                        Iterator v2 = v0_1.iterator();
                        do {
                            if (v2.hasNext()) {
                                Object v0_2 = v2.next();
                                if (v0_2 == null) {
                                    continue;
                                }
                                if (!Boolean.parseBoolean(((String) v0_2))) {
                                    continue;
                                }
                                if (Logger.debug()) {
                                    Logger.v("SSCookieHandler", "X-SS-No-Cookie " + v1);
                                }
                                v0_3 = Collections.emptyMap();
                                return v0_3;
                            }
                            break;
                        } while (true);
                    }
                }

//                label_28:
                String v0_4 = this.a.getCookie(arg6.toString());
                if (v0_4 == null) {
                    return Collections.emptyMap();
                }

                if (v0_4.length() <= 0) {
                    Collections.emptyMap();
                }

                if (Logger.debug()) {
                    Logger.v("SSCookieHandler", "send cookie: " + v1 + " " + v0_4);
                }

                List v1_1 = Collections.singletonList(v0_4);
                v0_3 = Collections.emptyMap();
                if (arg7 != null) {
                    List v2_1 = a(arg7, "Cookie");
                    if (v2_1 != null && !v2_1.isEmpty()) {
                        return v0_3;
                    }

                    return Collections.singletonMap("Cookie", v1_1);
                }
                return Collections.singletonMap("Cookie", v1_1);
            } catch (Exception v0) {
                v0.printStackTrace();
            }

            v0_3 = Collections.emptyMap();
        } else {
            v0_3 = Collections.emptyMap();
        }
        return v0_3;
    }

    public final void put(URI arg10, Map arg11) throws IOException {
        if (arg10 != null && arg11 != null && this.a != null) {
            String v2 = arg10.toString();
            String[] v3 = new String[]{"Set-Cookie"};
            int v1;
            for (v1 = 0; v1 <= 0; ++v1) {
                String v4 = v3[v1];
                Object v0 = arg11.get(v4);
                if (v0 == null || (((List) v0).isEmpty())) {
                    v0 = arg11.get(v4.toLowerCase());
                    if (v0 != null && !((List) v0).isEmpty()) {

                    } else {
                        continue;
                    }
                }
//                label_23:
                Iterator v5 = ((List) v0).iterator();
                while (v5.hasNext()) {
                    v0 = v5.next();
                    this.a.setCookie(v2, ((String) v0));
                    if (!Logger.debug()) {
                        continue;
                    }
                    Logger.v("SSCookieHandler", "receive cookie: " + v2 + " " + v4 + ": " + (((String) v0)));
                }
            }
        }
    }
}

